package com.lntea.jdbc.spring;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.List;

import javax.sql.DataSource;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.object.MappingSqlQuery;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;

import com.lntea.jdbc.spring.bean.GoodsStock;

@RunWith(SpringRunner.class)
@ContextConfiguration(locations={"classpath:com/lntea/jdbc/spring/jdbc-template.xml"})
public class SqlQueryTest {
	
	@Autowired
	private DataSource ds;

	@Test
	public void testMappingSqlQuery(){
		GoodsStockMappingSqlQuery goodsStockQuery = new GoodsStockMappingSqlQuery(ds);
		Object[] params = {1};
		List<GoodsStock> gsList = goodsStockQuery.execute(params);
		if(gsList.size() > 0){
			System.out.println(gsList.get(0));
		}
		
		GoodsStock gs = goodsStockQuery.findObject(1);
		System.out.println(gs);
	}
	
	class GoodsStockMappingSqlQuery extends MappingSqlQuery<GoodsStock> {
		
		public GoodsStockMappingSqlQuery(DataSource ds) {
			super(ds, "select id, warehouse_id from t_wms_goods_stock where id = ?");
			super.declareParameter(new SqlParameter("id", Types.INTEGER));
			compile();
		}

		@Override
		protected GoodsStock mapRow(ResultSet rs, int rowNum) throws SQLException {
			GoodsStock gs = new GoodsStock();
			gs.setId(rs.getInt("id"));
			gs.setWarehouseId(rs.getLong("warehouse_id"));
			return gs;
		}
		
	}
}
